package com.itheima.bos.base.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.base.action.common.BaseAction;
import com.itheima.bos.base.service.FixedAreaService;
import com.itheima.bos.domain.FixedArea;
import com.opensymphony.xwork2.ActionContext;

import cn.itcast.crm.domain.Customer;
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class FixedAreaAction extends BaseAction<FixedArea>{
	
	//注入servic
	@Autowired
	private FixedAreaService fixedAreaService;
	
	@Action(value="fixedArea_save",results={@Result(name="success",
			location="./pages/base/fixed_area.html",type="redirect")})
	public String save(){
		
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	
	//分页查询  如果type为json   那么就不用写location  谁请求就会返回给谁
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//构造分页查询对象
		Pageable pageable = new PageRequest(page-1, rows);
		//构造  条件查询 对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				
				if(StringUtils.isNotBlank(model.getId())){
					//根据定区编号查询
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							model.getId());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					//根据公司模糊查询
					Predicate p2 = cb.like(root.get("company").as(String.class),
							"%"+model.getCompany()+"%");
					list.add(p2);
					
				}
				return cb.and(list.toArray(new Predicate[0]));
				
			}
		};
		
		// 调用业务层，查询数据
		Page<FixedArea> pageData = fixedAreaService.findPageData(specification,
						pageable);
		//System.out.println(pageData);
		//System.out.println("------------------------<<<<<<<<<<<<<<<<");

		// 压入值栈
	   pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
    
	
	//查询未关联定区列表
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomers(){
		//使用webClient调用webservice 接口
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8082/crm_management/services/customerService/noAssociationCustomers").accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		
		return SUCCESS;
	}
	//查询关联定区列表
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers",results={@Result(name="success",type="json")})
	public String findHasAssociationFixedAreaCustomers(){
		//使用webClient调用webservice 接口
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8082/crm_management/services/customerService/associationFixedAreaCustomers/"+model.getId()).accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		
		return SUCCESS;
	}
	
}
